/* ----------------------------------------------------------------------------------------------------------------
 * TiledScenery
 * ----------------------------------------------------------------------------------------------------------------
 */
package pure.engine.scenery 
{
	import flash.display.BitmapData;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import pure.engine.world.TiledWorldContent;
	
	/**
	 * TiledMap
	 * @author Pure
	 */
public class TiledScenery extends AbstractScenery 
{
	public function TiledScenery(renderData:Array, sheet:BitmapData, tileSize:int, type:String = "steady")
	{
		super(type);
		
		_tileSize = tileSize;
		
		_renderData = renderData;
		_sheet = sheet;
		_numTileInSheetRow = sheet.width / _tileSize;

		cachedRect.width = _tileSize;
		cachedRect.height = _tileSize;
	}
	
	
	
	// ----------------------------------［ Model ］----------------------------------
	
	protected var _renderData:Array;
	
	protected var _sheet:BitmapData;

	protected var _numTileInSheetRow:int;    // 源图中每行位块数目
	
	protected var _tileSize:int;

	
	// ----------------------------------［ Cache ］----------------------------------

	protected static var cachedRect:Rectangle = new Rectangle();
	
	protected static var cachedPoint:Point = new Point();


	
	
	override public function destroy():void
	{
		super.destroy();
		
		_sheet = null;
		_renderData = null;
	}
	
	
	
	/* ----------------------------------------------------------------------------------------------------------------
	 * ★★★★★★★★★★★★★★★★★★★★★  private  ★★★★★★★★★★★★★★★★★★★★★
	 * ----------------------------------------------------------------------------------------------------------------
	 */
	
	
	override protected function _drawScenery():void
	{
		lenX = _renderData[0].length;
		lenY = _renderData.length;
		
		_sceneryBuffer = new BitmapData(lenX * _tileSize, lenY * _tileSize, true, 0);
		
		var lenX:int, lenY:int, i:int, j:int;

		for (i = 0; i < lenY; i++)
		{
			for (j = 0; j < lenX; j++) 
			{
				var index:int = _renderData[i][j];

				// Tile渲染数据为-1，跳过绘制
				if (index == -1) 
				{
					continue;
				}

				cachedRect.x = (index % _numTileInSheetRow) * _tileSize;
				cachedRect.y = int(index / _numTileInSheetRow) * _tileSize;
				
				cachedPoint.x = j * _tileSize;
				cachedPoint.y = i * _tileSize;
				
				_sceneryBuffer.copyPixels(_sheet, cachedRect, cachedPoint, null, null, true);
			}
		}
	}
}

}